package leecode

import "sort"

func getMinimumDifference(root *TreeNode) int {
	if root == nil {
		return 0
	}
	var nums []int
	// 左 中 右
	var minDiff func(node *TreeNode)
	minDiff = func(node *TreeNode) {
		if node == nil {
			return
		}
		minDiff(node.Left)
		nums = append(nums, node.Val)
		minDiff(node.Right)
	}
	minDiff(root)
	var diff []int
	for i := 0; i+1 < len(nums); i = i + 1 {
		diff = append(diff, nums[i+1]-nums[i])
	}
	sort.Ints(diff)
	return diff[0]
}
